initialize();

//初始化
function initialize() {

	//查询按钮点击事件绑定
	$(".btn-warning").click(function() {
		checkList();
	})

	//删除按钮点击事件绑定
	$(".btn-danger").click(function() {
		deleteItem();
	});

	//编辑按钮点击事件绑定
	$(".btn-success").click(function() {
		editItem();
	})

	//确认修改按钮点击事件绑定
	$(".btn-modify").click(function() {
		modify();
	})

	//确认添加按钮点击事件绑定
	$(".btn-submit").click(function() {
		comfirmSubmit();
	})

	$('#addForm').bootstrapValidator({
		fields: { /*验证*/
			customer_name1: { /*键名和input name值对应*/
				validators: {
					notEmpty: { /*非空提示*/
						message: 'customer_name1不能为空'
					}
				}
			},
			contact_name1: {
				validators: {
					notEmpty: {
						message: 'contact_name1不能为空'
					}
				}
			},
			phone1: {
				validators: {
					notEmpty: {
						message: 'phone1不能为空'
					}
				}
			},
			address1: {
				validators: {
					notEmpty: {
						message: 'address1不能为空'
					}
				}
			},
			province1: {
				validators: {
					notEmpty: {
						message: 'province1不能为空'
					}
				}
			},
			city1: {
				validators: {
					notEmpty: {
						message: 'city1不能为空'
					}
				}
			},
			sale_employee_id1: {
				validators: {
					notEmpty: {
						message: 'sale_employee_id1不能为空'
					}
				}
			},
			credit_limit1: {
				validators: {
					notEmpty: {
						message: 'credit_limit1不能为空'
					}
				}
			},
		}
	});

	$('#modifyForm').bootstrapValidator({
		fields: { /*验证*/
			customer_name: { /*键名和input name值对应*/
				validators: {
					notEmpty: { /*非空提示*/
						message: 'customer_name不能为空'
					}
				}
			},
			contact_name: {
				validators: {
					notEmpty: {
						message: 'contact_name不能为空'
					}
				}
			},
			phone: {
				validators: {
					notEmpty: {
						message: 'phone不能为空'
					}
				}
			},
			address: {
				validators: {
					notEmpty: {
						message: 'address不能为空'
					}
				}
			},
			province: {
				validators: {
					notEmpty: {
						message: 'province不能为空'
					}
				}
			},
			city: {
				validators: {
					notEmpty: {
						message: 'city不能为空'
					}
				}
			},
			sale_employee_id: {
				validators: {
					notEmpty: {
						message: 'sale_employee_id不能为空'
					}
				}
			},
			credit_limit: {
				validators: {
					notEmpty: {
						message: 'credit_limit不能为空'
					}
				}
			},
		}
	});
}

//ajax异步查询
function checkList() {
	$.ajax({
		type: "get",
		url: "/customer/search",
		async: true,
		success: function(data) {
			var result = JSON.parse(data);
			$('#userlist').DataTable({
				"data": result.data,
				"columns": [{
						"data": "0"
					},
					{
						"data": "1"
					},
					{
						"data": "2"
					},
					{
						"data": "3"
					},
					{
						"data": "4"
					},
					{
						"data": "5"
					},
					{
						"data": "6"
					},
					{
						"data": "7"
					},
					{
						"data": "8"
					}
				]
			});
			checkOneRow();
		},
		error: function(data) {
			console.log(data);
		},
	});
}

//ajax异步删除
function deleteItem() {
	var id = $(".selected>.sorting_1").html();
	$.ajax({
		type: "post",
		url: "/customer/delete",
		data: {
			"id": id,
		},
		async: true,
		success: function(data) {
			console.log(data);
			var info = JSON.parse(data);
			alert(info.message);
			if(info.error == 0) {
				var table = $('#userlist').DataTable();
				table.row('.selected').remove().draw(false);
			}
		},
		error: function(data) {
			console.log(data);
		}
	})
}

//编辑按钮
function editItem() {
	var customer_id = $(".selected>.sorting_1").html();
	var customer_name = $(".selected td").eq(1).html();
	var contact_name = $(".selected td").eq(2).html();
	var phone = $(".selected td").eq(3).html();
	var address = $(".selected td").eq(4).html();
	var province = $(".selected td").eq(5).html();
	var city = $(".selected td").eq(6).html();
	var sale_employee_id = $(".selected td").eq(7).html();
	var credit_limit = $(".selected td").eq(8).html();
	$("#customer_id").val(customer_id);
	$("#customer_name").val(customer_name);
	$("#contact_name").val(contact_name);
	$("#phone").val(phone);
	$("#address").val(address);
	$("#province").val(province);
	$("#city").val(city);
	$("#sale_employee_id").val(sale_employee_id);
	$("#credit_limit").val(credit_limit);
}

//ajax异步确定修改按钮
function modify() {
	var customer_id = $("#customer_id").val();
	var customer_name = $("#customer_name").val();
	var contact_name = $("#contact_name").val();
	var phone = $("#phone").val();
	var address = $("#address").val();
	var province = $("#province").val();
	var city = $("#city").val();
	var sale_employee_id = $("#sale_employee_id").val();
	var credit_limit = $("#credit_limit").val();
	$.ajax({
		type: "post",
		url: "/customer/modify",
		async: true,
		data: {
			"customer_id": customer_id,
			"customer_name": customer_name,
			"contact_name": contact_name,
			"phone": phone,
			"address": address,
			"province": province,
			"city": city,
			"sale_employee_id": sale_employee_id,
			"credit_limit": credit_limit,
		},
		success: function(data) {
			if(data) {
				alert("修改成功");
				$(".btn-close-modal").click();
			} else {
				alert("修改失败");
			}
		},
		error: function(data) {
			console.log(data);
		}
	});
}

//ajax异步确认添加按钮
function comfirmSubmit() {
	var customer_name = $("#customer_name1").val();
	var contact_name = $("#contact_name1").val();
	var phone = $("#phone1").val();
	var address = $("#address1").val();
	var province = $("#province1").val();
	var city = $("#city1").val();
	var sale_employee_id = $("#sale_employee_id1").val();
	var credit_limit = $("#credit_limit1").val();
	$.ajax({
		type: "post",
		url: "/customer/insert",
		async: true,
		data: {
			"customer_name": customer_name,
			"contact_name": contact_name,
			"phone": phone,
			"address": address,
			"province": province,
			"city": city,
			"sale_employee_id": sale_employee_id,
			"credit_limit": credit_limit,
		},
		success: function(data) {
			if(data) {
				alert("增加成功");
				$(".btn-close-modal1").click();
			} else {
				alert("增加失败");
			}
		},
		error: function(data) {
			console.log(data);
		}
	});
}

//选中单行
function checkOneRow() {
	var table = $('#userlist').DataTable();
	$('#userlist tbody').on('click', 'tr', function() {
		if($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			table.$('tr.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});
}